
#pragma once
#include <d3dx9.h>
#include "../../TwMath/twmath.h"
#include "../TwHeadFile.h"
#include "../pick/TwPick.h"


struct TerrainVertex
{
	float x, y, z;
	enum FVF
	{
		FVF_Flags = D3DFVF_XYZ
	};
};

struct FocusVertex
{
	float x, y, z, rhw;
	DWORD color;
	float u, v;

	enum FVF
	{
		FVF_Flags = (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
	};

	FocusVertex()
		:z(0)
		,rhw(1.f)
		,color(0xFFFFFFFF)
	{}
};

class TwRoundBox
{
	TwVector3d  m_MinPoint;
	TwVector3d  m_MaxPoint;
};


class TwTerrainSection
{
public:
	TwTerrainSection():bIsVisible(true){};
public:
	bool  bIsVisible;
	TwRect3d    m_Box;
	TwVector3d  m_nPos;
	D3DXVECTOR4	m_OffPos;
};
enum TerrainTexIndex
{
	eTerrHeightMap = 0,
	eTerrTex1,
	eTerrTex2,
	eTerrTex3,
	eTerrTex4,
	eTerrTex5,
};
class TwFocus
{
public:
	TwFocus():m_pDevice(0),m_focusTex(0){};
	~TwFocus(){};
	void  render();
	void  SetDevice(void* pkDevoce);
	void SetFocusData(const D3DXVECTOR3& min3,const D3DXVECTOR3& max3);

	void Update();
	LPDIRECT3DDEVICE9 m_pDevice;

	FocusVertex       m_pkFocusVertex[4];
	LPDIRECT3DTEXTURE9	   m_focusTex;

protected:
private:

};

class TwTerrain   _BaseClass_(TwRefRoot)
{

public:

	TwTerrain(void);
	~TwTerrain(void);

	bool Init( float fWidth, UINT uiHeightmapLevel, float fMaxHeight,LPDIRECT3DDEVICE9 pDevice );

	void Render();

	void InitLight();

	void EffectRender();

	void RenderTerrain();

	void CreatePixelShader();

	void RenderEx();

	void ComputerIsVisible();

	void SetTerrainTexTure(void* pkMap,int nIndex);

	const D3DXVECTOR3 GetCurSecPos() const {return m_d3dCur;}
	const bool		  IsSelSection() {return m_bSel;}

public:

	LPDIRECT3DDEVICE9 m_pDevice;
	LPDIRECT3DVERTEXBUFFER9 m_pTerrainVB;
	LPDIRECT3DINDEXBUFFER9 m_pTerrainIB;
	D3DMATERIAL9 m_TerrainMaterial;

	LPDIRECT3DTEXTURE9 m_pHeightMap;
	LPDIRECT3DTEXTURE9 m_pTerrainTex1;
	LPDIRECT3DTEXTURE9 m_pTerrainTex2;
	LPDIRECT3DTEXTURE9 m_pTerrainTex3;
	LPDIRECT3DTEXTURE9 m_pTerrainTex4;
	LPDIRECT3DTEXTURE9 m_pTerrainTex5;

	D3DXVECTOR3 m_d3dCur;
 
	float m_fWidth;								
	float m_fMaxHeight;							
	float m_fTileLength;						
	UINT m_uiNumXTile;							
	UINT m_uiNumYTile;							
	UINT m_uiNumTiles;							
	UINT m_uiNumVertices;						
	UINT m_uiNumIndices;		

	LPD3DXEFFECT m_pkeffect;    
	D3DXHANDLE   m_pktechnique;

	TerrainVertex		m_VerTex[16*16];
	TwTerrainSection*	m_pkSection;

	TwPick     m_mPick;
	bool	   m_bSel;

	TwFocus    m_Focus;
};
